<?php

class class_counter
{
	//Khai bao bien
		
	
	function class_counter()	
	{
		//Khoi tao bien
			
	}
	
	function Total_Online()
	{
		global $dbconn;
		
		$sql = "select * from whos_online";
		$rs = new db_query($dbconn, $sql);
		
		return $rs->numrows();
	}
	function Total_Count()
	{
		global $dbconn;
		$sql = "select hits from counter";
		return get_values($sql);
	}
	
	function update_whos_online() {
		global $customer_id, $HTTP_SESSION_VARS;
		global $dbconn;
		
		$wo_customer_id = $HTTP_SESSION_VARS['session_user_login']['user_id'];
		$wo_full_name = $HTTP_SESSION_VARS['session_user_login']['user_name'];
		
		if($wo_customer_id < 1){
			$wo_full_name = "guest";
		}
		
		
		$wo_session_id = session_id();
		//echo "wo_session_id: $wo_session_id<br>";
		$wo_ip_address = getenv('REMOTE_ADDR');
		$wo_last_page_url = getenv('REQUEST_URI');
		
		$current_time = time();
		$xx_mins_ago = ($current_time - 900);
		
		// remove entries that have expired
		
		//tep_db_query("delete from whos_online where time_last_click < '" . $xx_mins_ago . "'");
		$query = "delete from whos_online where time_last_click < '" . $xx_mins_ago . "'";
		$dbconn->execute ($query);
		
		$sql = "select count(*) as count from whos_online where session_id = '" . $wo_session_id . "'";
		$khach_online = get_values($sql);
		
		if ($khach_online > 0) {
			$query = "update whos_online set customer_id = '" . (int)$wo_customer_id . "', full_name = '" . $wo_full_name . "', ip_address = '" . $wo_ip_address . "', time_last_click = '" . $current_time . "', last_page_url = '" . $wo_last_page_url . "' where session_id = '" . $wo_session_id . "'";
		} else {
			$query = "insert into whos_online (customer_id, full_name, session_id, ip_address, time_entry, time_last_click, last_page_url) values ('" . (int)$wo_customer_id . "', '" . $wo_full_name . "', '" . $wo_session_id . "', '" . $wo_ip_address . "', '" . $current_time . "', '" . $current_time . "', '" . $wo_last_page_url . "')";
		}
		$dbconn->execute ($query);
	}
	
	function update_counter()
	{
		global $dbconn;
		$wo_session_id = session_id();
		$sql = "select count(*) as count from whos_online where session_id = '" . $wo_session_id . "'";
		$khach_online = get_values($sql);
		if ($khach_online == 0){
			//tang so dem
			$query = "update counter set hits = hits + 1";
			$dbconn->execute ($query);
		}
	}
	
	function HienThi()
	{
		global $langid;
		$str_counter = "";
		$tructuyen = $this->Total_Online();
		$sotruycap = $this->Total_Count();
		//Noi dung
		if($langid == 0){
			$str_counter = <<<EOT
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><font color="#ffffff"><b>L&#432;&#7907;t truy c&#7853;p</b></font></td>
  </tr>
  <tr>
    <td align="center"><font color="#FFff00"><b>$sotruycap</b></font></td>
  </tr>
</table>			
EOT;
}else{
			$str_counter = <<<EOT
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><font color="#000000"><b>Visitor</b></font></td>
  </tr>
  <tr>
    <td align="center"><font color="#FF0000"><b>$sotruycap</b></font></td>
  </tr>
</table>
EOT;
}//end if
		
		return $str_counter;
	}//end function HienThi


}//end class_counter

?>
